var  Dialog = $('<div title=""></div>');
$(document).ready(function(){
   
    });


function showPhones(){
    $('#addActivity').delegate('','click', function() {
        $('#addActivity').parent().parent().append('<div class="parentShowDelete"><div class="formRight " style="position: relative;"><span class="span3"><span class="formNote">Type</span><select id="customer_activity_id_" class="chzn-select" style="width: 100%;" data-placeholder=" - - Select - - " name="customer[activity_id_]">'+$('#customer_activity_id_0').html()+'</select></span><span class="span3"><span class="formNote">Size</span><input type="text" id="customer_activity_size_0" name="customer[activity_size_0]"></span><div class="showDeleteIcon delModifyLess" onClick="deletePhone(this)"></div></div></div>');
        updateActivityGrid();
        $(".chzn-select").chosen();
    });
    $('#addContact').delegate('','click', function() {
        $('#addContact').parent().parent().append('<div class="formRight Contacts" style="position: relative;"><div class="showDelete4Contact" onClick="deleteContact(this)"></div><div class="formRight contact">Contact 1</div><div class="formRight"><span class="span2"><span class="formNote">Title</span> <select id="customer_title_id_contact_999" class="chzn-select" style="width: 100%;" data-placeholder=" - - Select - - " name="customer[title_id_contact_999]">'+$('#customer_title_id').html()+'</select></span><span class="span6"><span class="formNote">Name</span><input type="text" id="customer_name_contact_999" name="customer[name_contact_999]"> </span></div><div class="formRight"><span class="span6"><span class="formNote">Position</span><input type="text" id="customer_position_contact_999" name="customer[position_contact_999]"></span></div><div class="formRight"><span class="span6"><span class="formNote">E-Mail</span><input type="text" id="customer_email_contact_999" name="customer[email_contact_999]"></span></div><div class="parentShowDelete"><div class="formRight " style="position: relative;"><span class="span6"><span class="formNote">Phone / mobile</span><input type="text" id="customer_phone_contact_999_0" name="customer_phone_contact_999_0]">  </span><div class="showAddIcon addModify" onClick="addPhone(this)"></div></div></div></div>');
        updateContactGrid();
        updatePhoneGrid();
        $(".chzn-select").chosen();
    });
}

function addPhone(caller){
    $(caller).parent().parent().after('<div class="parentShowDelete">'+$(caller).parent().parent().html()+'</div>');
    showDeleteIcones(caller);
    updatePhoneGrid();
    caller.event.stopPropagation();
}

function showDeleteIcones(caller){
    x = ($(caller).parent().parent().next().find(".showDeleteIcon").length > 0)?'':'<div class="showDeleteIcon delModify" onClick="deletePhone(this)"></div>';
    $(caller).parent().parent().next().children(':first').append(x);
}

function deletePhone(caller){
    $(caller).parent().parent().remove();
    updatePhoneGrid();
    updateActivityGrid();
    showDeleteIcones(caller);
}

function deleteContact(caller){
    $(caller).parent().remove();
    updateContactGrid();
    updatePhoneGrid();
}


function updateContactGrid(){
    var num = 1;
    $('.contact').each(function(){
        $(this).html('Contact '+num); 
        num++;
    });
   
    var i = 0;
    var iphone = 0;
    $('input[id^=customer_name_contact_]').each(function(){ 
        $(this).attr('id','customer_name_contact_'+i);
        $(this).attr('name','customer[name_contact_'+i+']');
        iphone = 0;
        $(this).parent().parent().parent().find('input[id^=customer_phone_contact_]').each(function(){ 
            $(this).attr('id','customer_phone_contact_'+i+'_'+iphone);
            $(this).attr('name','customer[phone_contact_'+i+'_'+iphone+']');
            iphone++;
        });
        i++;
    });
    var i2 = 0;
    $('select[id^=customer_title_id_contact_]').each(function(){ 
        $(this).attr('id','customer_title_id_contact_'+i2);
        $(this).attr('name','customer[title_id_contact_'+i2+']');
        i2++;
    });
    var i3 = 0;
    $('input[id^=customer_position_contact_]').each(function(){ 
        $(this).attr('id','customer_position_contact_'+i3);
        $(this).attr('name','customer[position_contact_'+i3+']');
        i3++;
    });
    var i4 = 0;
    $('input[id^=customer_email_contact_]').each(function(){ 
        $(this).attr('id','customer_email_contact_'+i4);
        $(this).attr('name','customer[email_contact_'+i4+']');
        i4++;
    });

}

function updatePhoneGrid(){
    var index = 0;
    $('input[id^=customer_phone_owner]').each(function(){ 
        $(this).attr('id','customer_phone_owner_'+index);
        $(this).attr('name','customer[phone_owner_'+index+']');
        index++;
    });
    var index1 = 0;
    $('input[id^=customer_phone_consultant]').each(function(){ 
        $(this).attr('id','customer_phone_consultant_'+index1);
        $(this).attr('name','customer[phone_consultant_'+index1+']');
        index1++;
    });
    var index2 = 0;
    $('input[id^=customer_phone_key_person]').each(function(){ 
        $(this).attr('id','customer_phone_key_person_'+index2);
        $(this).attr('name','customer[phone_key_person_'+index2+']');
        index2++;
    });
    var index3 = 0;
    for($i=0; $i<($('input[id^=customer_name_contact_]')).length; $i++){
        index3 = 0;
        $('input[id^=customer_phone_contact_'+$i+']').each(function(){ 
            $(this).attr('id','customer_phone_contact_'+$i+'_'+index3);
            $(this).attr('name','customer[phone_contact_'+$i+'_'+index3+']');
            index3++;
        });
    }
  
}

function updateActivityGrid(){
    var index = 0;
    $('select[id^=customer_activity_id_]').each(function(){ 
        $(this).attr('id','customer_activity_id_'+index);
        $(this).attr('name','customer[activity_id_'+index+']');
        index++;
    });
    var index1 = 0;
    $('input[id^=customer_activity_size_]').each(function(){ 
        $(this).attr('id','customer_activity_size_'+index1);
        $(this).attr('name','customer[activity_size_'+index1+']');
        index1++;
    });
  
}


function showAllCustomers(){
    var url = $("#url4SowAllCustomers").val();
    $.ajax ({
        type: "GET",
        url: url,
        success: function (html)
        {
            $('#DAH').html(html);
            $('#DataTables_Table_0').dataTable({   
                bJQueryUI: true,
                sPaginationType: "full_numbers"
            });
            
            $('.tips').tipsy();
        }
    });
}

function addCustomer(type,showSucessBar){
    var url = $("#url4AddCustomer").val();
    if(!type){
        $.ajax ({
            type: "GET",
            url: url,
            success: function (html)
            {
                $('#DAH').html(html);
                $(".chzn-select").chosen();
                showPhones();
                if(showSucessBar)
                    $('#DAH').prepend('<div class="nNote nSuccess hideit"><p><strong>SUCCESS: </strong> Customer added successfully. </p></div>');     
            }
        });
    }else{
        var contact=0;
        var str = '';
        $('.Contacts').each(function(){
            str += "&"+contact +"="+$('[id^=customer_phone_contact_'+contact+'_]').length
            contact++;
        });
                 
                 
        var data = $("#form").serialize() 
        + "&activity=" +  $('input[id^=customer_activity_size_]').length
        + "&owner=" +  $('input[id^=customer_phone_owner_]').length
        + "&consultant=" +  $('input[id^=customer_phone_consultant_]').length
        + "&key_person=" +  $('input[id^=customer_phone_key_person_]').length
        + "&contact_number=" + contact 
        + str;
                 
        $.ajax ({
            type: "POST",
            url: url,
            data :data,
            success: function (html)
            {
                if(html == "Done"){
                    addCustomer(false, true);
                    $('html, body').animate({
                        scrollTop:$('#rightSide').position().top
                    }, 'slow');
                }
                else{
                    $('#DAH').html(html);
                    $(".chzn-select").chosen();
                    showPhones();
                }
            }
        });
    }
}



    
function showCustomerbrief(customerId){
    var url = $("#url4CustomerBrief").val();
    var data = '&customerId='+customerId;
    $.ajax ({
        type: "GET",
        url: url,
        data :data,
        success: function (html)
        {
            Dialog.html(html)    
            .dialog({
                title: 'Information ...',
                modal:true,
                autoOpen: false,
                width: 500
            });
            Dialog.dialog('open');
        }
    });
}

function deleteCustomer(customerId){
    $('<div></div>').appendTo('body')
    .html('<div><h6>Are you sure? you want to Delete this customer !! </h6></div>')
    .dialog({
        modal: true, 
        title: 'Delete Customer', 
        zIndex: 10000, 
        autoOpen: true,
        width: 500, 
        resizable: false,
        buttons: {
            Yes: function () {
                $.ajax ({
                    type: "GET",
                    url: $("#url4CustomerDelete").val(),
                    data : '&customerId='+customerId,
                    success: function (html)
                    {
                        showAllCustomers();
                    }
                });
                $(this).dialog("close");
            },
            No: function () {
                $(this).dialog("close");
            }
        },
        close: function (event, ui) {
            $(this).remove();
        }
    });
}

function showCustomer(customerId){
    var url = $("#url4showCustomer").val();
    var data = 'customerId='+customerId;
    $.ajax ({
        type: "GET",
        url: url,
        data :data,
        success: function (html)
        {
            $('#DAH').html(html);
            var percent = $('#bar5').attr('title');
            $('#bar5').animate({width: percent},2000); 
            $('.tipS').tipsy();
            $('.tipN').tipsy();
            $('.tipW').tipsy();
        }
    });
}

function editCustomer(customerId, actions){
    var url = $("#url4editCustomer").val();
    var data = $("#form").serialize() + '&customerId='+customerId+'&actions='+actions;
    $.ajax ({
        type: (actions == 'save')?"POST":"GET",
        url: url,
        data :data,
        success: function (html)
        {
            $('#customerDiv').html(html);
            $(".tipsy").remove();
            $('.tipS').tipsy();
            $('.tipN').tipsy();
            $('.tipW').tipsy();
        }
    });
}